/*    */ package com.zimbra.cs.servlet;
/*    */ 
/*    */ import com.zimbra.common.service.ServiceException;
/*    */ import com.zimbra.common.util.Log;
/*    */ import com.zimbra.common.util.ZimbraCookie;
/*    */ import com.zimbra.common.util.ZimbraLog;
/*    */ import com.zimbra.cs.account.Account;
/*    */ import com.zimbra.cs.account.AuthToken;
/*    */ import com.zimbra.cs.account.AuthTokenException;
/*    */ import com.zimbra.cs.account.Provisioning;
/*    */ import com.zimbra.cs.service.AuthProvider;
/*    */ import javax.servlet.ServletRequest;
/*    */ import javax.servlet.ServletResponse;
/*    */ import javax.servlet.http.Cookie;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import org.eclipse.jetty.http.PathMap;
/*    */ import org.eclipse.jetty.security.ServerAuthException;
/*    */ import org.eclipse.jetty.security.UserAuthentication;
/*    */ import org.eclipse.jetty.security.authentication.BasicAuthenticator;
/*    */ import org.eclipse.jetty.server.Authentication;
/*    */ import org.eclipse.jetty.server.UserIdentity;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class ZimbraAuthenticator
/*    */   extends BasicAuthenticator
/*    */ {
/* 47 */   protected String urlPattern = "";
/*    */   
/*    */   public String getUrlPattern() {
/* 50 */     return this.urlPattern;
/*    */   }
/*    */   
/*    */   public void setUrlPattern(String urlPattern) {
/* 54 */     this.urlPattern = (urlPattern == null ? null : urlPattern.replace("//", "/"));
/*    */   }
/*    */   
/*    */   public Authentication validateRequest(ServletRequest req, ServletResponse resp, boolean mandatory)
/*    */     throws ServerAuthException
/*    */   {
/* 60 */     if ((mandatory) && ((req instanceof HttpServletRequest))) {
/* 61 */       HttpServletRequest httpReq = (HttpServletRequest)req;
/*    */       
/*    */ 
/*    */ 
/*    */ 
/* 66 */       if (PathMap.match(this.urlPattern, httpReq.getRequestURI())) {
/* 67 */         Cookie[] cookies = httpReq.getCookies();
/*    */         
/* 69 */         if (cookies != null) {
/* 70 */           for (Cookie cookie : cookies) {
/* 71 */             if ((ZimbraCookie.authTokenCookieName(true).equalsIgnoreCase(cookie.getName())) || (ZimbraCookie.authTokenCookieName(false).equalsIgnoreCase(cookie.getName())))
/*    */             {
/* 73 */               String encoded = cookie.getValue();
/*    */               try
/*    */               {
/* 76 */                 AuthToken token = AuthProvider.getAuthToken(encoded);
/* 77 */                 Account authAcct = AuthProvider.validateAuthToken(Provisioning.getInstance(), token, false);
/* 78 */                 if (authAcct != null) {
/* 79 */                   if ((this._loginService instanceof ZimbraLoginService)) {
/* 80 */                     UserIdentity user = ((ZimbraLoginService)this._loginService).makeUserIdentity(authAcct.getMail());
/* 81 */                     ZimbraLog.security.debug("Auth token validated");
/* 82 */                     return new UserAuthentication(getAuthMethod(), user);
/*    */                   }
/* 84 */                   ZimbraLog.security.warn("Misconfigured? _loginService not ZimbraLoginService");
/* 85 */                   if (!$assertionsDisabled) throw new AssertionError();
/*    */                 }
/*    */               }
/*    */               catch (AuthTokenException e) {
/* 89 */                 ZimbraLog.security.error("Unable to authenticate due to AuthTokenException", e);
/*    */               } catch (ServiceException e) {
/* 91 */                 ZimbraLog.security.error("Unable to authenticate due to ServiceException", e);
/*    */               }
/*    */             }
/*    */           }
/* 95 */           ZimbraLog.security.debug("no valid auth token, fallback to basic");
/*    */         }
/*    */       }
/*    */     }
/* 99 */     return super.validateRequest(req, resp, mandatory);
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/servlet/ZimbraAuthenticator.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */